package data.dictionary.builder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;

import data.dictionary.Dictionary_Memory;

import tools.Pair;

public class DictionaryBuilderFromFile {
	private String filename;

	public DictionaryBuilderFromFile(String filename) {
		super();
		this.filename = filename;
	}

	public Dictionary_Memory<String> build() {
		try {
			FileReader fr = new FileReader(new File(filename));

			BufferedReader in = new BufferedReader(fr);

			//
			Dictionary_Memory<String> dico = load(in);
			//System.out.println("COUCOU FIN");
			in.close();

			return dico;
		} catch (IOException e) {
			System.err.println("Can't open file " + filename
					+ " for reading... Loading aborted");

		} catch (Exception e) {
			System.err.println("Invalid Format : " + filename
					+ "... Loading aborted");

		}

		return null;
	}

	public Dictionary_Memory<String> load(BufferedReader in) {
		Dictionary_Memory<String> dico = new Dictionary_Memory<String>();
		try {
			while (true) {

				String buf;

				buf = in.readLine();
				
				//System.out.println("COUCOU "+ buf);
				
				if (buf == null){
					//System.out.println("COUOCU ");
					break;
				}
//				StringTokenizer st = new StringTokenizer(buf);
//				dico.addValue(st.nextToken());
				dico.addValue(buf);
			}
			
			return dico;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}
}
